package com.stmp.webSocket.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Auther: zhaoss
 * @Date: 2022/12/23 - 12 - 23 - 17:24
 * @Description: com.stmp.webSocket.config
 * @version: 1.0
 */
@Controller
@Slf4j
public class SocketController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @GetMapping("/stomp/soket/{message}")
    public void sendMessage(@PathVariable String message){
        log.info("收到的消息是: {}",message);
        simpMessagingTemplate.convertAndSend("/topic/stomp/data","我已收到你的消息: ".concat(message));
    }

    @MessageMapping("/send")
    @SendTo()
    public void sendToUser(@RequestBody Map<String,String> map ) throws Exception {

        Thread.sleep(1000); // simulated delay
          log.info("收到的消息是: ",map);
        simpMessagingTemplate.convertAndSend("/topic/stomp/data","我已收到你的消息: ".concat(map.toString()));
    }


}
